﻿using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace AT.IBMS.Framework.Data
{

    /// <summary>
    /// 数据访问提供程序。
    /// </summary>
    public partial class DataProvider
    {

        private Database database;

        internal DataProvider(string connectionStringName)
        {
            database = DatabaseFactory.CreateDatabase(connectionStringName);
        }

        /// <summary>
        /// 创建当前连接的 Connection 对象。
        /// </summary>
        /// <returns></returns>
        public DbConnection CreateConnection()
        {
            return database.CreateConnection();
        }

        /// <summary>
        /// 获取当前 DataProvider 对象的数据库连接字符串。
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return database.ConnectionString;
            }
        }

        /// <summary>
        /// 返回实现 DbCommand 类的提供程序的类的一个新实例。
        /// </summary>
        /// <param name="commandType">指示或指定如何解释 CommandText 属性。</param>
        /// <param name="commandText">获取或设置针对数据源运行的文本命令。</param>
        /// <returns>DbCommand 的新实例。</returns>
        public virtual DbCommand CreateCommand(
            string commandText = "",
            CommandType commandType = CommandType.Text
            )
        {
            DbCommand command;

            command = database.DbProviderFactory.CreateCommand();
            command.CommandType = commandType;
            command.CommandText = commandText;

            return command;

        }

        /// <summary>
        /// 返回实现 DbParameter 类的提供程序的类的一个新实例。
        /// </summary>
        /// <param name="name">要映射的参数的名称。</param>
        /// <param name="dbType">DbType 值之一。</param>
        /// <param name="size">参数的长度。</param>
        /// <param name="direction">ParameterDirection 值之一。</param>
        /// <param name="sourceColumn">源列的名称。</param>
        /// <param name="sourceVersion">DataRowVersion 值之一。</param>
        /// <param name="value">一个 Object，它是 DbParameter 的值。</param>
        /// <returns>DbParameter 的新实例。</returns>
        public virtual DbParameter CreateParameter(
            string name = "",
            object value = null ,
            DbType dbType = DbType.String,
            int size = 0,
            ParameterDirection direction = ParameterDirection.Input,
            string sourceColumn = "",
            DataRowVersion sourceVersion = DataRowVersion.Current
            )
        {
            DbParameter parameter;

            parameter = database.DbProviderFactory.CreateParameter();

            parameter.ParameterName = name;
            parameter.DbType = dbType;
            parameter.Size = size;
            parameter.Direction = direction;
            parameter.SourceColumn = sourceColumn;
            parameter.SourceVersion = sourceVersion;
            parameter.Value = value;

            return parameter;
        }

    }

}
